package com.example.controller;

import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.atomic.AtomicLong;

@RestController
@CrossOrigin(origins = "*")
public class ApiController {

    private Map<Long, Map<String, Object>> users = new HashMap<>();
    private AtomicLong postIdCounter = new AtomicLong(1000);

    public ApiController() {
        Map<String, Object> user1 = new HashMap<>();
        user1.put("id", 1L);
        user1.put("name", "张三");
        user1.put("email", "zhangsan@example.com");
        user1.put("phone", "138-0013-8000");
        user1.put("username", "zhangsan");
        user1.put("website", "zhangsan.org");

        Map<String, Object> address = new HashMap<>();
        address.put("street", "长安街");
        address.put("suite", "A座 1001");
        address.put("city", "北京市");
        address.put("zipcode", "100000");

        Map<String, Object> geo = new HashMap<>();
        geo.put("lat", "39.9042");
        geo.put("lng", "116.4074");
        address.put("geo", geo);

        user1.put("address", address);

        Map<String, Object> company = new HashMap<>();
        company.put("name", "北京科技有限公司");
        company.put("catchPhrase", "科技改变生活");
        company.put("bs", "互联网服务");
        user1.put("company", company);

        users.put(1L, user1);
    }

    @GetMapping("/users/{id}")
    public Map<String, Object> getUser(@PathVariable Long id) {
        System.out.println("收到GET请求，用户ID: " + id);

        Map<String, Object> user = users.get(id);
        if (user == null) {
            Map<String, Object> defaultUser = new HashMap<>();
            defaultUser.put("id", id);
            defaultUser.put("name", "默认用户" + id);
            defaultUser.put("email", "user" + id + "@example.com");
            defaultUser.put("phone", "000-0000-0000");
            defaultUser.put("username", "user" + id);
            defaultUser.put("website", "example.org");
            return defaultUser;
        }

        return user;
    }

    @PostMapping("/posts")
    public Map<String, Object> createPost(@RequestBody Map<String, Object> postData) {
        System.out.println("收到POST请求，数据: " + postData);

        String title = (String) postData.get("title");
        String body = (String) postData.get("body");
        Object userId = postData.get("userId");

        Map<String, Object> response = new HashMap<>();
        response.put("id", postIdCounter.incrementAndGet());
        response.put("title", title != null ? title : "默认标题");
        response.put("body", body != null ? body : "默认内容");
        response.put("userId", userId != null ? userId : 1);

        System.out.println("返回数据: " + response);
        return response;
    }

    @GetMapping("/health")
    public Map<String, Object> health() {
        Map<String, Object> response = new HashMap<>();
        response.put("status", "success");
        response.put("message", "服务运行正常");
        response.put("timestamp", System.currentTimeMillis());
        return response;
    }
}